#!/bin/bash
# Copyright (c) 2019 Presto Labs Pte. Ltd.
# Author: jaewon

export TZ=UTC

DATE=${1}
MACHINE=${2}
SUB_REQ=${3}
WORKER_ID=${4}
GROUP=${5}

grep "^${DATE},${MACHINE},${SUB_REQ},${WORKER_ID},${GROUP}," status &> /dev/null
if [ ${?} -eq 0 ]; then
  echo "Skip ${DATE},${MACHINE},${SUB_REQ},${WORKER_ID},${GROUP}"
  exit 0
fi

mkdir -p "log/${DATE}"
LOG_FILE="log/${DATE}/log-${DATE}-${MACHINE}-${SUB_REQ}-${WORKER_ID}"

BEGIN_T="$(date '+%Y-%m-%d %H:%M:%S')"

time python -m coin.feed.fastfeed.feed_cache_gen \
  --feed_cache_dir=/remote/iosg/coin-2/buckets/feed.derived.feed_cache.coin \
  --machine=${MACHINE} \
  --sub_req=${SUB_REQ} \
  --worker_id=${WORKER_ID} \
  --date=${DATE} \
  --group="${GROUP}" \
  --quiet &> "${LOG_FILE}"

RET=${?}
END_T="$(date '+%Y-%m-%d %H:%M:%S')"

echo "${DATE},${MACHINE},${SUB_REQ},${WORKER_ID},${GROUP},${BEGIN_T},${END_T},${LOG_FILE},${RET}" >> status
exit ${RET}
